package com.zw.fzbilibili.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zw.fzbilibili.dao.SysUserDao;
import com.zw.fzbilibili.entity.SysUser;
import com.zw.fzbilibili.enums.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *
 * </p>
 *
 * @author zhangwen
 * @since 2022/12/23
 */
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private SysUserDao sysUserDao;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername, s); // 1.用户名
        queryWrapper.eq(SysUser::getStatus, StatusEnum.STATUS_0); // 2.未删除状态
        SysUser user = sysUserDao.selectOne(queryWrapper);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return user;
    }
}
